package com.taxi.driver.module.order.price;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qianxx.utils.TypeUtils;
import com.taxi.driver.R;
import com.taxi.driver.common.AppConfig;
import com.taxi.driver.common.BaseActivity;
import com.taxi.driver.config.IConstants;
import com.taxi.driver.data.entity.OrderCostEntity;
import com.taxi.driver.module.order.price.dagger.DaggerPriceDetailComponent;
import com.taxi.driver.module.order.price.dagger.PriceDetailModule;
import com.taxi.driver.module.web.WebActivity;
import com.taxi.driver.widget.ImageViewPlus;
import com.taxi.driver.widget.TextViewPlus;

import java.io.Serializable;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 功能描述：费用详情页
 */
public class PriceDetailActivity extends BaseActivity implements PriceDetailContract.View {

    public static void actionStart(Context context, String orderUuid, OrderCostEntity entity) {
        Intent intent = new Intent(context, PriceDetailActivity.class);
        intent.putExtra(IConstants.ORDER_UUID, orderUuid);
        intent.putExtra(IConstants.PARAMS, entity);
        Log.i("order--", "orderUuid:" + orderUuid + ",totalFare:" + entity.totalFare);
        context.startActivity(intent);
    }

    public static void actionStart(Context context, String orderUuid, boolean isRealtime, double totalPrice) {
        Intent intent = new Intent(context, PriceDetailActivity.class);
        intent.putExtra(IConstants.ORDER_UUID, orderUuid);
        intent.putExtra(IConstants.CONFIG, isRealtime);
        intent.putExtra(IConstants.PRICE, totalPrice);
        context.startActivity(intent);
    }

    @BindView(R.id.tv_price)
    TextView mTvPrice;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.tv_rule)
    TextViewPlus mTvRule;
    @BindView(R.id.iv_close)
    ImageViewPlus mIvClose;
    @BindView(R.id.ll_additional)
    LinearLayout mLlAdditional;


    @Inject
    PriceDetailPresenter mPresenter;
    PriceDetailAdapter mAdapter;
    private String mOrderUuid = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_price_detail);
        ButterKnife.bind(this);
        DaggerPriceDetailComponent.builder().appComponent(getAppComponent())
                .priceDetailModule(new PriceDetailModule(this)).build().inject(this);

        mOrderUuid = getIntent().getStringExtra(IConstants.ORDER_UUID);
        mPresenter.setOrderUuid(mOrderUuid);
        mAdapter = new PriceDetailAdapter(this, R.layout.item_order_cost);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
        Serializable obj = getIntent().getSerializableExtra(IConstants.PARAMS);
        OrderCostEntity entity = new OrderCostEntity();
        if (obj != null) {
            entity = (OrderCostEntity) obj;
        } else {
            double price = getIntent().getDoubleExtra(IConstants.PRICE, 0);
            entity.totalFare = price;
        }
        setDisplay(entity); //控制显示

        mLlAdditional.setVisibility(View.GONE);

        boolean isRealtime = getIntent().getBooleanExtra(IConstants.CONFIG, false);
        if (AppConfig.isSpecial()) { //[专车]
            if (isRealtime) {
                mPresenter.getRealtimeFare(); //(专车)获取实时费用
            } else {
                mPresenter.reqOrderFare(); //(专车)获取费用明细
            }
        } else { //[出租车]
            mPresenter.reqFareItems(); //(出租车)获取费用详情
            //隐藏计价规则路口
            mTvRule.setVisibility(View.GONE);
            mTvRule.setEnabled(false);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.subscribe();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPresenter.unsubscribe();
    }

    @OnClick({R.id.iv_close, R.id.tv_rule})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_close:
                finish(); //关闭界面
                break;
            case R.id.tv_rule:
                String rulesUrl = AppConfig.HOST
                        + mPresenter.getPriceRules() + "&orderUuid=" + mOrderUuid;

                Log.i("url--", rulesUrl + "");
                if (TextUtils.isEmpty(rulesUrl)) {
                    toast("未获取到计价规则");
                    return;
                }
                WebActivity.actionStart(this, rulesUrl, "计价规则");
                break;
        }
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public void setDisplay(OrderCostEntity entity) {
        if (entity == null) return;
        mTvPrice.setText(TypeUtils.getValue2(entity.totalFare) + "元");
        mAdapter.setAll(entity.costItems);
    }

}
